import { ref, computed } from 'vue'
import { defineStore } from 'pinia'
import router from '@/router'
import { useKeepAliveStore } from './keepAlive'

const keepAliveStore = useKeepAliveStore()

export const useTabsStore = defineStore(
  'sv-tabs',
  () => {
    const tabsList = ref([])

    // 添加标签页
    function addTab(tabItem) {
      const isExist = tabsList.value.some((item) => item.path === tabItem.path)
      if (!isExist) {
        tabsList.value.push(tabItem)
      }

      // 添加到 keepAlive
      if (tabItem.isKeepAlive) {
        keepAliveStore.addKeepAlive(tabItem.name)
      }
    }

    // 移除标签页
    function removeTab(path, isCurrent = true) {
      if (isCurrent) {
        tabsList.value.forEach((item, index) => {
          if (item.path !== path) return
          const nextTab = tabsList.value[index + 1] || tabsList.value[index - 1]
          if (!nextTab) return
          router.push(nextTab.path)
        })
      }

      const tabItem = tabsList.value.find((item) => item.path === path)

      // 移除 keepAlive
      if (tabItem.isKeepAlive) {
        keepAliveStore.removeKeepAlive(tabItem.name)
      }

      // 更新 tabsList
      tabsList.value = tabsList.value.filter((item) => item.path !== path)
    }

    // 设置标签页
    function setTabs(tabs = []) {
      tabsList.value = tabs
    }

    /**
     * 关闭标签页
     * @param {String} type all | cur | other | left | right
     * @param {String} path 当前标签页路径
     */
    function closeTabs(type, path) {
      const tabItem = tabsList.value.find((item) => item.path === path)
      const tabIndex = tabsList.value.findIndex((item) => item.path === path)

      if (type === 'all') {
        tabsList.value = tabsList.value.filter((item) => {
          return item.path === path || !item.close
        })
        const KeepAliveList = tabsList.value.filter((item) => item.isKeepAlive)
        keepAliveStore.setKeepAlive(KeepAliveList.map((item) => item.name))
      } else if (type === 'cur') {
        if (!tabItem.close) return
        removeTab(path, true)
      } else if (type === 'other') {
        tabsList.value = tabsList.value.filter((item) => {
          return item.path === path || !item.close
        })
        const KeepAliveList = tabsList.value.filter((item) => item.isKeepAlive)
        keepAliveStore.setKeepAlive(KeepAliveList.map((item) => item.name))
      } else if (type === 'left') {
        if (tabIndex !== -1) {
          const range = [0, tabIndex]
          tabsList.value = tabsList.value.filter((item, index) => {
            return index < range[0] || index >= range[1] || !item.close
          })
          const KeepAliveList = tabsList.value.filter((item) => item.isKeepAlive)
          keepAliveStore.setKeepAlive(KeepAliveList.map((item) => item.name))
        }
      } else if (type === 'right') {
        if (tabIndex !== -1) {
          const range = [tabIndex + 1, tabsList.value.length]
          tabsList.value = tabsList.value.filter((item, index) => {
            return index < range[0] || index >= range[1] || !item.close
          })
          const KeepAliveList = tabsList.value.filter((item) => item.isKeepAlive)
          keepAliveStore.setKeepAlive(KeepAliveList.map((item) => item.name))
        }
      }
    }

    return {
      tabsList,
      addTab,
      removeTab,
      setTabs,
      closeTabs
    }
  },
  {
    persist: true
  }
)
